package com.seafwg.educenter.controller;


import com.seafwg.commonutils.JwtUtils;
import com.seafwg.commonutils.R;
import com.seafwg.educenter.entity.UcenterMember;
import com.seafwg.educenter.entity.vo.RegisterVo;
import com.seafwg.educenter.service.UcenterMemberService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 会员表 前端控制器
 * </p>
 *
 * @author seafwg
 * @since 2021-04-07
 */
@RestController
@CrossOrigin
@RequestMapping("/educenter/member")
public class UcenterMemberController {

    @Autowired
    private UcenterMemberService memberService;

    /**
     * 用户登录
     * @param member
     * @return
     * [注意] post对应@RequestBody,get对应@Pathvarble
     */
    @PostMapping("/login")
    public R loginUser(@RequestBody UcenterMember member) {
        // member对象封装手机号和密码
        // 调用service方法实现登录
        // 返回token值，使用jwt生成
        String token = memberService.login(member);
        return R.ok().data("token", token);
    }

    /**
     * 用户注册
     * @param registerVo
     * @return
     */
    @PostMapping("/register")
    public R registerUser(@RequestBody RegisterVo registerVo) {
        memberService.register(registerVo);
        return R.ok();
    }

    @GetMapping("/getMemberInfo")
    public R getMemberInfo(HttpServletRequest httpServletRequest) {
        // 调用jwt工具类的方法。根据request对象获取头信息，返回用户id
        String memberIdByJwtToken = JwtUtils.getMemberIdByJwtToken(httpServletRequest);
        // 查询数据库根据用户id获取用户信息
        UcenterMember ucenterMember = memberService.getById(memberIdByJwtToken);
        // 返回用户信息
        return R.ok().data("userInfo", ucenterMember);
    }

}

